package com.shuoyutech.dp.common.framework.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author dyb
 * @date 2021/2/25
 */
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    protected T data;
    protected String resultCode;
    protected String message;

    public static <T> Result<T> succeed() {
        return succeedWith(null, CodeEnum.SUCCESS.getCode());
    }

    public static <T> Result<T> succeed(T data) {
        return succeedWith(data, CodeEnum.SUCCESS.getCode());
    }

    public static <T> PageResult<T> succeed(IPage<T> page) {
        return new PageResult<>(page.getRecords(), CodeEnum.SUCCESS.getCode(), null, (int) page.getCurrent(), (int) page.getSize(), page.getTotal());
    }

    public static <T> Result<T> succeedWith(T data, String resultCode) {
        return new Result<>(data, resultCode, null);
    }

    public static <T> Result<T> error(String message) {
        return failedWith(null, message);
    }

    public static <T> Result<T> error(T data, String message) {
        return failedWith(data, message);
    }

    public static <T> Result<T> failedWith(T data, String message) {
        return new Result<>(data, CodeEnum.ERROR.getCode(), message);
    }
}
